<?php
/**
 * Created by PhpStorm.
 * User: gogo
 * Date: 2019/2/22
 * Time: 下午5:33
 */
namespace app\model;


class Area extends BaseModel
{
    function addData($data){

        try{
            $post = [
                'name'=> $data['name']?:'',
                'fullname'=> $data['fullname'],
                'pinyin'=> $data['pinyin']?:'',
                'lat'=> $data['lat'],
                'lng'=> $data['lng'],
                'parent_code'=> $data['parent_code'],
                'code'=> $data['code'],
                'level'=> $data['level']
            ];

            $id = db('area')->where(['code'=> $post['code']])->value('id');
            if($id){
                db('area')->where(['id'=> $id])->update($post);
            }else{
                db('area')->insert($post);
            }

        }catch (\Exception $exception){
//            echo $exception;
        }

    }

    // 根据上级code获取子地区
    static function getChildren($parent_code = 0){

//        $list = db('area')->where(['parent_code'=> $parent_code])->field('code, name, fullname')->select();
        $list = db('area_tag')->where(['parent_id'=> $parent_code])->field('id as code, name')->select();

        return array_merge([[
            'code'=> 0,
            'name'=> '不限'
        ]], $list);

    }
}